<!DOCTYPE html>
<html lang="zh-cmn-Hans">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
		<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
		<meta http-equiv="Pragma" content="no-cache" />
		<meta http-equiv="Expires" content="0" />
		<title>ChinaBang Awards 2017</title>
		<link rel="stylesheet" href="../style/weui.css" />
		<link rel="stylesheet" href="./example.css" />
	</head>

	<body ontouchstart>
		<div class="weui-toptips weui-toptips_warn js_tooltips">错误提示</div>
		<div class="container" id="container"></div>
		<div class="page" style="opacity:1">
			<div class="page__hd">
				 <h1 class="page__title">
        			<img src="images/logo.png" alt="ChinaBang" height="21px" />
   				 </h1>
   				 <p class="page__desc"></p>
    
				<!--<h1 class="page__title"></h1>
				<p class="page__desc">ChinaBang Awards 2017</p>-->
			</div>
			<div class="page__bd">
				<div class="weui-panel weui-panel_access">
					<div class="weui-panel__hd">入围公司简介</div>
        			<div class="weui-panel__bd">
        				<a href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg">
			                <div class="weui-media-box__hd">
			                    <img class="weui-media-box__thumb" src="" alt="" id="img">
			                </div>
			                <div class="weui-media-box__bd">
			                    <h4 class="weui-media-box__title" id="item_title"></h4>
			                    <p class="weui-media-box__desc" id="item_company"></p>
			                </div>
		                </a>
		                <div class="weui-media-box weui-media-box_text" >
                			<p class="weui-media-box__desc" id="item_detail" style="height: 60px;"></p>
            			</div>
            		</div>
            		<div class="weui-panel__ft">
            			<a href="javascript:void(0)" class="weui-cell weui-cell_access weui-cell_link" id="show_detail">
	              			<div class="weui-cell__bd" >前往 NodeData 上查看更多详细信息</div>
	                		<span class="weui-cell__ft"></span>
            			</a>
        			</div>
            	</div>
		</div>
		<div class="weui-msg__opr-area">
			<p class="weui-msg__desc" id="vote_result" style="text-align: center;"></p>
	        <p class="weui-btn-area">
	            <a href="javascript:void(0);" class="weui-btn weui-btn_primary" id="item_action"></a>
	        </p>
   		</div>
   		<div class="weui-msg__extra-area" style="position: relative; margin-top: 5%;">
			<div class="weui-footer">
				<p class="weui-footer__links">
					<a href="javascript:void(0);" class="weui-footer__link" id="index_page">返回 ChinaBang Awards 2017 投票首页</a>
				</p>
				<p class="weui-footer__text">Copyright &copy; 2017 TechNode</p>
			</div>
			
			<div id="dialogs">
	            <!--BEGIN dialog2-->
	            <div class="js_dialog" id="iosDialog2" style="display: none;">
	                <div class="weui-mask"><img src="images/cb/share.png" alt="分享" height="480" width="375" /></div>
	                <div class="weui-dialog cb-share">
	                    <div class="weui-dialog__ft">
	                        <a href="javascript:;" class="weui-dialog__btn weui-dialog__btn_primary">知道了</a>
	                    </div>
	                </div>
	            </div>
	            <!--END dialog2-->
       		 </div>
		</div>
   		<input id="gimg" type="hidden" value="http://angelnode.qiniudn.com/18d0d795cd31493bb7850e6a41ee3c7d.jpg?imageView2/1/w/169/h/169">
		<input id="name" type="hidden" value=" 入围了CBA年度评选，快来为他投一票">
		<input id="desc" type="hidden" value="ChinaBang Awards 2017 年度评选 | by TechNode">
		<script src="js/util/jquery-1.8.3.min.js"></script>
		<script src="js/util/jweixin-1.0.0.js"></script>
		<script src="./weui.min.js"></script>
		<script src="js/util/configs.js"></script>
		<script src="js/util/restUrls.js"></script>
		<script src="js/util/utils.js"></script>
		

		<script type="text/javascript">
			$(function() {
				var winH = $(window).height();
				var categorySpace = 10;
				var itemId = utils.getUrlParam("itemId");
				var title = utils.getUrlParam("title");
				var voterId = localStorage.getItem("voterId") == null ? utils.getUrlParam("id") : localStorage.getItem("voterId");
				var accessToken = localStorage.getItem("accessToken") == null ? utils.getUrlParam("accessToken") : localStorage.getItem("accessToken");
				
				var $iosDialog1 = $('#iosDialog1'),
	            $iosDialog2 = $('#iosDialog2'),
	            $androidDialog1 = $('#androidDialog1'),
	            $androidDialog2 = $('#androidDialog2');
	
		        $('#dialogs').on('click', '.weui-dialog__btn', function(){
		            $(this).parents('.js_dialog').fadeOut(200);
		        });
		
		        $('#showIOSDialog1').on('click', function(){
		            $iosDialog1.fadeIn(200);
		        });
		        
		        $('#showAndroidDialog1').on('click', function(){
		            $androidDialog1.fadeIn(200);
		        });
		        $('#showAndroidDialog2').on('click', function(){
		            $androidDialog2.fadeIn(200);
		        });
        
				$("#index_page").on("click", function () {
						window.location.href = "category.html?voterId="+voterId+"&accessToken="+accessToken;
				});
				if(!voterId || !accessToken) {
					var link_ = getWeChatUrl(title, itemId);
					console.log(link_);
					window.location.href = link_;
					return;
				} else {
					$(".page__desc").html(title);
					localStorage.setItem("voterId", voterId);
					localStorage.setItem("accessToken", accessToken);
					getItemDetail(voterId, accessToken, itemId);
				}
			});
			
			
			
			
			function getImageUrl(imgName) {
				if(imgName == undefined || imgName == '') {
					return "";
				} else {
					return "http://angelnode.qiniudn.com/" + imgName + "?imageView2/1/w/169/h/169";
				}
			}
			
			function getWeChatUrl(title, itemId) {
				var jsonParam = {"title":title, "itemId":itemId};
				var params = "?params="+JSON.stringify(jsonParam);
//				var params = "?params="+itemId+","+title;
				var redirectUrl = encodeURI(restUrls.vote.getVoter+ params);
				return restUrls.weChat.prefix + "http%3A%2F%2F" + redirectUrl + restUrls.weChat.last;
			}
			
			function getItemDetail(voterId, accessToken, itemId) {
				$.post(restUrls.vote.queryItemDetail + "?voterId=" + voterId + "&accessToken=" + accessToken, {itemId:itemId}, function(result) {
					if(result.errorCode == 0) {
						if(result.data && result.data.rsData) {
							var itemDetail = result.data.rsData[0];
							$("#item_title").html(itemDetail.name);
							$("#item_detail").html(itemDetail.detail);
							$("#img").prop("src", getImageUrl(itemDetail.logo));
							$("#show_detail").on("click", function() {
								window.location.href = itemDetail.jumpUrl;
							});
							var flag = itemDetail.result;
							if(!flag || flag == 0) {
								$("#item_action").html("投一票");
								$("#item_action").on("click", function() {
									var itemId = itemDetail.id;
									$.post(restUrls.vote.poll + "?voterId=" + voterId + "&accessToken=" + accessToken, {itemId:itemId}, function(result) {
										if(result.errorCode == 0) {
											var item = result.data.rsData[0];
											var words = "感谢您为 "+item.name+" 投上第 "+ item.count+" 票";
											$("#vote_result").html(words);
											$("#item_action").html("分享");
											$("#item_action").unbind("click");
											$("#item_action").on("click", function() {
												$iosDialog2 = $('#iosDialog2'),
												$iosDialog2.fadeIn(200);
											});	
										} else if(result.errorCode == 10001) { 
											weui.alert("投票项目不存在");
										} else if(result.errorCode == 10002) { 
											weui.alert("投票未开始");
										} else if(result.errorCode == 10003) { 
											weui.alert("投票已结束");
										} else if(result.errorCode == 10004) {
												weui.alert("该项目已投过票");
										} else if(result.errorCode == 10005) { 
											weui.alert("该奖项投票次数已达上限");
										} else if(result.errorCode == 10006) { 
											weui.alert("该投票活动不存在");
										} else if(result.errorCode == 10010) {
												weui.alert("用户被禁用");
										} else if(result.errorCode == 30001) { 
											localStorage.setItem("voterId", null);
											localStorage.setItem("accessToken", null);
											window.location.reload();
										} else if(result.errorCode == 90000) {
												weui.alert("请求失败");
										}
									}, "json");
								});
							} else {
								var item = result.data.rsData[0];
								$("#item_action").html("分享");
								$("#item_action").unbind("click");
								$("#item_action").on("click", function() {
									$iosDialog2 = $('#iosDialog2'),
									$iosDialog2.fadeIn(200);
								});
								
							}
						}
					} else if(result.errorCode == 30001) { 
							weui.alert("参数错误");
					} else if(result.errorCode == 90000) {
							weui.alert("请求失败");
					}
					$.ajax({
						type: "post",
						url: restUrls.vote.getConfig,
						data:{pageUrl:location.href.split('#')[0]},
						success: function(result) {
							if(result.data && result.data.rsData) {
								var wxConfig = result.data.rsData[0];
								$("#appId").val(wxConfig.appId);
								$("#timestamp").val(wxConfig.timestamp);
								$("#nonceStr").val(wxConfig.noncestr);
								$("#signature").val(wxConfig.signature);
								wx.config({
									appId: wxConfig.appId, // 必填，公众号的唯一标识
									timestamp: wxConfig.timestamp, // 必填，生成签名的时间戳
									nonceStr: wxConfig.noncestr, // 必填，生成签名的随机串
									signature: wxConfig.signature, // 必填，签名，见附录1
									jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
								});
		
								wx.ready(function() {
									var strurl = location.href.split('#')[0];
									var gimg = $("#gimg").val();
									var desc = $('#desc').val();
									var title = $("#item_title").html();
									var name = title + $('#name').val();
									wx.onMenuShareTimeline({
										title: name,
										desc: desc,
										link: strurl,
										imgUrl: gimg,
										trigger: function(res) {},
										success: function(res) {
											addShareRecord(itemId);
										},
										cancel: function(res) {
											// alert("已取消");
										},
										fail: function(res) {
											alert(JSON.stringify(res));
										}
									});
		
									wx.onMenuShareAppMessage({
										title: name,
										desc: desc,
										link: strurl,
										imgUrl: gimg,
										trigger: function(res) {},
										success: function(res) {
											addShareRecord(itemId);
										},
										cancel: function(res) {
											//alert("已取消");
										},
										fail: function(res) {
											alert(JSON.stringify(res));
										}
									});
								});
		
								wx.error(function(res) {
									alert(res.errMsg);
								});
							}
						}
					});
				}, "json");
			}
			
			function addShareRecord(itemId) {
				var voterId = localStorage.getItem("voterId");
				var accessToken = localStorage.getItem("accessToken");
				var url = restUrls.vote.addShareRecord + "?voterId="+voterId+"&accessToken="+accessToken;
				$.post(url, {itemId:itemId}, function(result) {
					if(result.errorCode == 0) {
						weui.alert("分享成功");
					}
				}, "json");
			}
		</script>
	</body>
</html>